package cn.lhl.mvc;

import cn.lhl.converter.Convert;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by datou on 2017/3/16.
 */
public class ActionProxy {
    public static String getActionClass(ActionMapping actionMapping) throws Exception {
        HttpServletRequest request=ServletActionContext.getRequest();
        Class clazz = Class.forName(actionMapping.getClassName());
        Object action=clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        Convert convert=new Convert();
        Map<String,String[]> parameterMap = request.getParameterMap();
        for (String item: parameterMap.keySet()) {
            if (assembling(item,action, parameterMap.get(item)[0], convert)) {
                String colName = item;
                colName = colName.replace(colName.charAt(0) + "", new String(colName.charAt(0) + "").toUpperCase());
                String methodName = "set" + colName;
                String value = parameterMap.get(item)[0];
                Field field = clazz.getDeclaredField(item);
                if (value != null) {
                    Method method = clazz.getMethod(methodName, field.getType());
                    method.invoke(action, convert.convertValue(value, field.getType()));
                }
            }
        }
        Method method=clazz.getMethod(actionMapping.getMethod());
        String result1 = method.invoke(action).toString();
        for (Field item : fields) {
            item.setAccessible(true);//设置私有属性可访问
            if( item.get(action)!=null) {
                request.setAttribute(item.getName(), item.get(action));
            }
        }
        return result1;
    }

    public static Boolean assembling(String string,Object clazz,String value,Convert convert) throws NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        int index=string.indexOf(".");
        if(index!=-1){
            String name=string.substring(0,index);
            Field field = clazz.getClass().getDeclaredField(name);
            String clazzName=String.valueOf(field.getType()).substring(String.valueOf(field.getType()).indexOf(" ")+1);
            Class aClass = Class.forName(clazzName);
            String str=string.substring(index+1);
            String colName=name;
            colName=colName.substring(0, 1).toUpperCase() + colName.substring(1);
            String methodName="set"+colName;
            Field field1 = clazz.getClass().getDeclaredField(name);
            field1.setAccessible(true);
            Object o;
            if(field1.get(clazz)!=null) {
                o=field1.get(clazz);
            }else{
                o=aClass.newInstance();
            }
            if(o!=null) {
                Method method = clazz.getClass().getMethod(methodName,field1.getType());
                method.invoke(clazz,o);
            }
            if(str.indexOf(".")!=-1){
                assembling(str,o,value,convert);
            }else{
                String colName1=str;
                colName1=colName1.substring(0, 1).toUpperCase() + colName1.substring(1);
                String methodName1="set"+colName1;
                System.out.println(str);
                System.out.println(colName1);
                Field field2 = aClass.getDeclaredField(str);
                if(value!=null) {
                    Method method = aClass.getMethod(methodName1,field2.getType());
                    method.invoke(o, convert.convertValue(value,field2.getType()));
                }
            }
            return false;
        }
        return true;
    }

}
